/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TextFrame.java
 *
 * Created on Oct 22, 2011, 8:30:20 PM
 */
package teletutor.blackboard.impl;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;

/**
 *
 * @author msi
 */
public class TextFrame extends javax.swing.JFrame {

    TextObject obj = null;
    ImageIcon paletteIcon = new ImageIcon(getClass().getResource("/palette.jpg"));
    BufferedImage paletteImage = new BufferedImage(60, 200, BufferedImage.TYPE_3BYTE_BGR);

    /** Creates new form TextFrame */
    public TextFrame(TextObject obj) {
        //super("Enter Text");
        initComponents();
        this.setTitle("Text Editor");
        this.obj = obj;        
        paletteImage.getGraphics().drawImage(paletteIcon.getImage(), 0, 0, null);      
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        fontBox = new javax.swing.JComboBox();
        fontSizeSpinner = new javax.swing.JSpinner();
        jColorChooser1 = new javax.swing.JColorChooser();
        colorPicker = new javax.swing.JLabel();

        setResizable(false);
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
                formWindowLostFocus(evt);
            }
        });
        addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                formFocusLost(evt);
            }
        });

        textArea.setColumns(20);
        textArea.setRows(5);
        textArea.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                textAreaKeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(textArea);

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        fontBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Arial", "Times New Roman", "Georgia", "Courier New", "Comic Sans MS" }));
        fontBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                fontBoxItemStateChanged(evt);
            }
        });

        fontSizeSpinner.setFont(new java.awt.Font("Arial", 0, 13)); // NOI18N
        fontSizeSpinner.setModel(new javax.swing.SpinnerNumberModel(16, 8, 64, 2));
        fontSizeSpinner.setEditor(new javax.swing.JSpinner.NumberEditor(fontSizeSpinner, ""));
        fontSizeSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                fontSizeSpinnerStateChanged(evt);
            }
        });

        colorPicker.setIcon(new javax.swing.ImageIcon(getClass().getResource("/palette.jpg"))); // NOI18N
        colorPicker.setText("jLabel1");
        colorPicker.setMaximumSize(new java.awt.Dimension(60, 200));
        colorPicker.setMinimumSize(new java.awt.Dimension(60, 200));
        colorPicker.setPreferredSize(new java.awt.Dimension(60, 200));
        colorPicker.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                colorPickerMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(fontBox, 0, 218, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(fontSizeSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(okButton, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 80, Short.MAX_VALUE)
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(colorPicker, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(colorPicker, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(fontBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(fontSizeSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(cancelButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(okButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                obj.setText(textArea.getText());
                obj.setShowBorder(false);
                obj.redraw();
                obj.hideProperties();
                obj.validate();
            }
        });


    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        obj.hideProperties();
        obj.invalidate();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void textAreaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textAreaKeyTyped
        if (obj != null) {
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    obj.setText(textArea.getText());
                    obj.redraw();
                }
            });

        }
    }//GEN-LAST:event_textAreaKeyTyped

    private void formFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusLost
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                obj.setText(textArea.getText());
                obj.setShowBorder(false);
                obj.redraw();
                obj.hideProperties();
            }
        });

    }//GEN-LAST:event_formFocusLost

    private void formWindowLostFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowLostFocus
        formFocusLost(null);

    }//GEN-LAST:event_formWindowLostFocus

    private void fontBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_fontBoxItemStateChanged
        obj.setFontName((String) fontBox.getSelectedItem());
        obj.redraw();
    }//GEN-LAST:event_fontBoxItemStateChanged

    private void fontSizeSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_fontSizeSpinnerStateChanged
        obj.setFontSize((int) fontSizeSpinner.getValue());
        obj.redraw();
    }//GEN-LAST:event_fontSizeSpinnerStateChanged

    private void colorPickerMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_colorPickerMouseClicked
       obj.setColor(new Color(paletteImage.getRGB(evt.getX(), evt.getY())));
       obj.redraw();
    }//GEN-LAST:event_colorPickerMouseClicked
    /**
     * @param args the command line arguments
    //     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new TextFrame().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel colorPicker;
    private javax.swing.JComboBox fontBox;
    private javax.swing.JSpinner fontSizeSpinner;
    private javax.swing.JColorChooser jColorChooser1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton okButton;
    javax.swing.JTextArea textArea;
    // End of variables declaration//GEN-END:variables
}
